package data.dao.daoImplementation;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import data.dao.*;
import data.dto.*;
import data.searchdto.*;

public class CompensationConsumedDAOImpl implements CompensationConsumedDAO {

	private ArrayList<CompensationLeaveUserDetails> compensationLeaveUserDetailsList;

	@Override
	public ArrayList<CompensationLeaveUserDetails> findCompensationLeave(
			CompensationConsumedSearchDTO compensationconsumedSearchDTO)
			throws NotFoundException, LeaveDAOException, SQLException {

		String sql = "SELECT C.userID,U.Username,C.numberofdays FROM compensationconsumed C, user U"
				+ " WHERE userID = ? AND C.userID = U.id ";
		PreparedStatement stmt = null;
		ArrayList<CompensationLeaveUserDetails> list = null;
		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);
			stmt.setInt(1, compensationconsumedSearchDTO.getUserid());

			list = constructObjects(stmt);

		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			util.Database.getInstance().rollBackConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			util.Database.getInstance().rollBackConnection();
		} finally {
			if (stmt != null)
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					util.Database.getInstance().rollBackConnection();
				}
			util.Database.getInstance().closeConnection();
		}
		return list;
	}

	@Override
	public ArrayList<CompensationLeaveUserDetails> findAllCompensationLeave()
			throws LeaveDAOException, SQLException {

		String sql = "SELECT C.userID,U.Username,C.numberofdays FROM compensationconsumed C, user U"
				+ " WHERE C.userID = U.id ";
		PreparedStatement stmt = null;
		ArrayList<CompensationLeaveUserDetails> list = null;
		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);

			list = constructObjects(stmt);

		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (stmt != null)
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			util.Database.getInstance().closeConnection();
		}
		return list;
	}

	@Override
	public void insertCompensationLeave(
			CompensationConsumedDTO compensationconsumedDTO)
			throws LeaveDAOException, SQLException {

		String sql = "";
		PreparedStatement stmt = null;

		try {
			sql = "INSERT INTO compensationsonsumed ( id, userID, numberofDays, "
					+ "createdOn, createdBy, modifiedOn, "
					+ "modifiedBy, status, isActive) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ";
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);

			stmt.setInt(1, compensationconsumedDTO.getId());
			stmt.setInt(2, compensationconsumedDTO.getUserid());
			stmt.setInt(3, compensationconsumedDTO.getNumberofdays());
			stmt.setTimestamp(4, util.CurrentDate.getCurrentTimeStamp());
			stmt.setInt(5, compensationconsumedDTO.getUserid());
			stmt.setTimestamp(6, util.CurrentDate.getCurrentTimeStamp());
			stmt.setInt(7, compensationconsumedDTO.getUserid());
			stmt.setInt(8, 1);
			stmt.setInt(9, 1);

			int rowcount = stmt.executeUpdate();
			if (rowcount != 1) {
				// System.out.println("PrimaryKey Error when updating DB!");
				throw new SQLException("PrimaryKey Error when updating DB!");
			}

		} finally {
			if (stmt != null)
				stmt.close();
			util.Database.getInstance().closeConnection();
		}

	}

	@Override
	public void updateCompensationLeave(
			CompensationConsumedDTO compensationconsumedDTO)
			throws LeaveDAOException, SQLException, NotFoundException {

		String sql = "UPDATE compensationsonsumed SET numberofDays = ?, "
				+ "modifiedOn = ?, modifiedBy = ?, "
				+ "status = ?,  WHERE id = ? AND userID = ?) ";
		PreparedStatement stmt = null;

		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);

			stmt.setInt(1, compensationconsumedDTO.getNumberofdays());
			stmt.setTimestamp(2, util.CurrentDate.getCurrentTimeStamp());
			stmt.setInt(3, util.CurrentUser.getInstance().getUserId());
			stmt.setInt(4, compensationconsumedDTO.getStatus());

			stmt.setInt(5, compensationconsumedDTO.getId());
			stmt.setInt(6, compensationconsumedDTO.getUserid());

			int rowcount = stmt.executeUpdate();
			if (rowcount == 0) {
				// System.out.println("Object could not be saved! (PrimaryKey not found)");
				throw new NotFoundException(
						"Object could not be saved! (PrimaryKey not found)");
			}
			if (rowcount > 1) {
				// System.out.println("PrimaryKey Error when updating DB! (Many objects were affected!)");
				throw new SQLException(
						"PrimaryKey Error when updating DB! (Many objects were affected!)");
			}
		} finally {
			if (stmt != null)
				stmt.close();
			util.Database.getInstance().closeConnection();
		}

	}

	@Override
	public void deleteCompensationLeave(
			CompensationConsumedDTO compensationconsumedDTO)
			throws LeaveDAOException, SQLException, NotFoundException {

		String sql = "DELETE FROM compensationsonsumed WHERE (id = ? ) ";
		PreparedStatement stmt = null;

		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);
			stmt.setInt(1, compensationconsumedDTO.getId());

			int rowcount = stmt.executeUpdate();
			if (rowcount == 0) {
				// System.out.println("Object could not be deleted (PrimaryKey not found)");
				throw new NotFoundException(
						"Object could not be deleted! (PrimaryKey not found)");
			}
			if (rowcount > 1) {
				// System.out.println("PrimaryKey Error when updating DB! (Many objects were deleted!)");
				throw new SQLException(
						"PrimaryKey Error when updating DB! (Many objects were deleted!)");
			}
		} finally {
			if (stmt != null)
				stmt.close();
			util.Database.getInstance().closeConnection();
		}

	}

	protected ArrayList<CompensationLeaveUserDetails> constructObjects(
			PreparedStatement stmt) throws NotFoundException,
			java.sql.SQLException {

		ResultSet result = null;

		try {
			result = stmt.executeQuery();
			compensationLeaveUserDetailsList = new ArrayList<CompensationLeaveUserDetails>();
			while (result.next()) {
				CompensationLeaveUserDetails valueObject = new CompensationLeaveUserDetails();
				valueObject.setUserid(result.getInt("userID"));
				valueObject.setNumberofdays(result.getInt("numberofDays"));
				valueObject.setUsername(result.getString("numberofDays"));
				compensationLeaveUserDetailsList.add(valueObject);
			}
		} catch (Exception e) {

		} finally {
			if (result != null)
				result.close();
			if (stmt != null)
				stmt.close();
			util.Database.getInstance().closeConnection();
		}
		return compensationLeaveUserDetailsList;
	}

}
